package com.cssl.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.Result;
import com.cssl.pojo.TbMember;
import com.cssl.service.TbMemberService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//@CrossOrigin
@RequestMapping("/member")
@RestController
public class MemberController {

    @Resource
    private TbMemberService tbMemberService;

    //会员添加
    @RequestMapping("/saveMember")
    public Result<TbMember> saveMember(TbMember tbMember) {
        System.out.println("会员添加:" + tbMember);

        //创建时间和修改时间默认为当前时间
        Date date = new Date();
        tbMember.setCreated(date);
        tbMember.setUpdated(date);

        tbMember.setIntegral(0);
        tbMember.setStatus(0);
        System.out.println("tbMember:" + tbMember);

        tbMemberService.save(tbMember);
        return Result.ok(tbMember);
    }

    //删除会员
    @RequestMapping("/removeMember")
    public Result<TbMember> removeMember(@RequestParam("id") Integer id) {
        System.out.println("删除会员:" + id);
        TbMember user = tbMemberService.getById(id);
        boolean b = tbMemberService.removeById(id);
        if (b){
            return Result.ok(user);
        }
        return null;
    }

    //修改会员信息
    @RequestMapping("/updateMember")
    public Result<TbMember> updateMember(TbMember tbMember) {
        System.out.println("修改会员:" + tbMember);
        //修改时间改为当前
        tbMember.setUpdated(new Date());
        //根据id改
        tbMemberService.updateById(tbMember);

        return Result.ok(tbMemberService.getById(tbMember.getId()));
    }

    //修改会员状态 0/1（可用/封号）
    @RequestMapping("/updateStatu")
    public int statuMember(@RequestParam("id") Integer id){
        TbMember tbMember = tbMemberService.getById(id);
        int status = tbMember.getStatus();

        UpdateWrapper<TbMember> updateWrapper = new UpdateWrapper<>();
        Integer i = null;

        if (status == 1){
            i = 0;
            updateWrapper.eq("id",id).set("status",i);

        }else{
            i = 1;
            updateWrapper.eq("id",id).set("status",i);
        }
        tbMemberService.update(updateWrapper);
        return i;
    }
    //查询会员信息
    @RequestMapping("/selectMember")
    public  Result<Page<TbMember>> selectMember(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pagesize") Integer pageSize,
                                       @RequestParam(value = "username",required = false) String username) {
        System.out.println("username:" + username);
        return Result.ok(tbMemberService.pageMember(pageNum, pageSize,username));
    }

}
